package com.hnqcgczy.service.impl;

import com.hnqcgczy.dao.IOrderDao;
import com.hnqcgczy.dao.imp.OrderDaoImpl;
import com.hnqcgczy.db.MyTransaction;
import com.hnqcgczy.service.IOrderService;
import com.hnqcgczy.vo.OrderDetailVo;
import com.hnqcgczy.vo.TOrder;
import com.hnqcgczy.vo.TOrderDetail;

import java.util.List;

public class OrderServiceImpl implements IOrderService {
    @Override
    public void createOrder(TOrder order, List<TOrderDetail> orderDetailList) {
        //事务 ， 手动提交
        MyTransaction myTransaction=new MyTransaction(false);
        //订单dao
        IOrderDao orderDao=new OrderDaoImpl(myTransaction);
        orderDao.addOrder(order);
        //循环添加一个或多个订单明细
        for (TOrderDetail detail : orderDetailList) {
            orderDao.addOrderDetail(detail);
        }
        //使用事务提交
        myTransaction.commit();
        //释放连接资源
        myTransaction.closeConn();
    }

    @Override
    public List<TOrder> getOrderList(String tuId) {
        MyTransaction myTransaction=new MyTransaction(true);
        IOrderDao orderDao=new OrderDaoImpl(myTransaction);
        List<TOrder> list=orderDao.getOrderList(tuId);
        if(list!=null&&list.size()>0){
            //通过订单编号获取订单明细
            for (TOrder tOrder : list) {
                List<OrderDetailVo> detailVos=orderDao.getOrderDetailList(tOrder.getToNumber());
                //将订单明细添加到订单对象的明细集合中
                tOrder.setDetailVoList(detailVos);
            }
        }
        myTransaction.closeConn();
        return list;
    }

    @Override
    public void updateOrder(TOrder order) {
        //事务 ， 手动提交
        MyTransaction myTransaction=new MyTransaction(false);
        //订单dao
        IOrderDao orderDao=new OrderDaoImpl(myTransaction);
        orderDao.updadeOrder(order);

        //使用事务提交
        myTransaction.commit();
        //释放连接资源
        myTransaction.closeConn();
    }

    @Override
    public TOrder getOrder(String oNumber) {
        MyTransaction myTransaction=new MyTransaction(true);
        IOrderDao orderDao=new OrderDaoImpl(myTransaction);
        TOrder order=orderDao.getOrder(oNumber);
        myTransaction.closeConn();
        return order;
    }
}
